using System;
using System.Collections.Generic;
using System.Text;
using CodePlex.Resourceful.Json;

namespace CodePlex.Resourceful.Metaweb
{

    // http://freebase.com/view/en/documentation
    public class MetawebClient : Client
    {
        public MetawebClient(string metawebUser)
            : base(new Uri("http://sandbox.freebase.com/api/service/"))
        {


#if !SILVERLIGHT
            this.CommonHeaders["X-Metaweb-Request"] = "asdf";
            this.CommonHeaders["Cookie"] = "metaweb-user=" + metawebUser;
#endif
        }

        //public MqlResponse MqlReadResponse(MqlBuilder requestBuilder)
        //{
        //    JsonObject o = this.MqlReadObject(requestBuilder);
        //    return new MqlResponse(o);
        //}

        public JsonObject MqlReadObject(MqlBuilder requestBuilder)
        {
            JsonReader r = this.MqlReadJson(requestBuilder);
            return new JsonObject(r);
        }

    

        public JsonReader MqlReadJson(MqlBuilder requestBuilder)
        {
            NameValuePairs p = new NameValuePairs();
            p["queries"] = requestBuilder.ToString();

            ClientResponse response = this.GetRepresentation(GetRequest.Create("mqlread", p));

            return new JsonReader(response.AsTextReader());

        }


        //public IEnumerable<string> GetTypes(string domain)
        //{

        //    MqlBuilder b = new MqlBuilder();

        //    using (b.StartQuery("qname"))
        //    {
        //        b.WriteProperty("type", "/type/domain");
        //        b.WriteProperty("id", domain);
        //        b.WriteProperty("types", new object[] { });
        //    }


        //    JsonReader r = this.MqlRead(b);

        //    bool inArray = false;
        //    while (r.Read())
        //    {

        //        if (r.TokenType == JsonToken.StartArray)
        //        {
        //            inArray = true;
        //        }
        //        else if (r.TokenType == JsonToken.EndArray)
        //        {
        //            inArray = false;
        //        }
        //        else if (inArray && r.TokenType == JsonToken.String)
        //        {
        //            yield return r.Value as string;
        //           // Console.WriteLine(r.Value);
        //        }
        //        // Console.WriteLine("{0,-20}{1,-20}{2}",r.TokenType, r.ValueType,r.Value);
        //    }


        //}



        //public IEnumerable<string> GetProperties(string type)
        //{
        //    MqlBuilder b = new MqlBuilder();

        //    using (b.StartQuery("qname"))
        //    {
        //        b.WriteProperty("type", "/type/type");
        //        b.WriteProperty("id", type);
        //        b.WriteProperty("properties", new object[] { });
        //    }


        //    JsonReader r = this.MqlRead(b);

        //    bool inArray = false;
        //    while (r.Read())
        //    {

        //        if (r.TokenType == JsonToken.StartArray)
        //        {
        //            inArray = true;
        //        }
        //        else if (r.TokenType == JsonToken.EndArray)
        //        {
        //            inArray = false;
        //        }
        //        else if (inArray && r.TokenType == JsonToken.String)
        //        {
        //            yield return r.Value as string;
        //            // Console.WriteLine(r.Value);
        //        }
        //        // Console.WriteLine("{0,-20}{1,-20}{2}",r.TokenType, r.ValueType,r.Value);
        //    }

        //}




        public ClientResponse MqlRead(MqlBuilder requestBuilder)
        {
            return Utils.MakeSynchronous<MqlBuilder, ClientResponse>(requestBuilder, MqlRead);
        }

        public void MqlRead(MqlBuilder requestBuilder, Action<ClientResponse> callback )
        {
            NameValuePairs p = new NameValuePairs();
            p["queries"] = requestBuilder.ToString();

            this.GetRepresentation(GetRequest.Create("mqlread", p), delegate(ClientResponse response)
            {
                callback(response);
            });
        }





    }

}
